/**
 * File: ReadLoadProcessor.java
 * Created by: mhaimel
 * Created on: 9 Jul 2009
 * CVS:  $Id: ReadLoadProcessor.java,v 1.3 2009/10/09 15:10:12 mhaimel Exp $
 */
package uk.ac.ebi.curtain.processor.load.impl;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

import uk.ac.ebi.curtain.utils.data.IdentifierProcessor;
import uk.ac.ebi.curtain.utils.data.impl.SolexaIdentifierProcessor;
import uk.ac.ebi.curtain.utils.file.EntryMapperBean;
import uk.ac.ebi.curtain.utils.file.FileInfo;
import uk.ac.ebi.velvet.model.Strand;

/**
 * @author mhaimel
 *
 */
public class ReadLoadProcessor extends AbstractLoadProcessor<Serializable, Serializable> {
	
	private final IdentifierProcessor idProcessor = new SolexaIdentifierProcessor();
	private final AtomicInteger iCnt = new AtomicInteger(0);

	@Override
	protected String buildReturnObject() {
		return "Finished with " + iCnt + " Reads loaded!";
	}

	@Override
	protected void addEntry(EntryMapperBean bean) {
		/* should not be called!!!*/
		throw new IllegalStateException("Should not be called");
	}

	private Strand getStrandInformation(String id) {
		return idProcessor.getStrand(id);
	}

	private String removeStrandInformation(String id) {
		return idProcessor.removeStrandInformation(id);
	}

	private String cleanId(String id) {
		return idProcessor.cleanId(id);
	}

	@Override
	protected boolean isValidEntry(FileInfo info, String id, long filePos) {
		String currId = id;
		Strand str = Strand.UNKOWN;
		if(info.getReadType().isPaired()){
			currId = removeStrandInformation(id);
			str = getStrandInformation(id);
		}else {
			currId = cleanId(id);
		}
		iCnt.incrementAndGet();
		addRead(info, currId, str, filePos);
		return false;
	}
}
